import { Camera, Vector2, Vector3 } from "three";

// 经纬度转三维向量
export const numArr2v3 = (lnglat: [number, number], z = 0): Vector3 => {
    return new Vector3(lnglat[0], lnglat[1], z)
}

// 世界坐标转屏幕坐标
export const getViewCp = (v3: Vector3, v2: Vector2, camera: Camera) => {
    var worldVector = v3.clone();
    var standardVector = worldVector.project(camera); //世界坐标转标准设备坐标
    var a = window.innerWidth / 2;
    var b = window.innerHeight / 2;
    var vx = Math.round(standardVector.x * a + a); //标准设备坐标转屏幕坐标
    var vy = Math.round(-standardVector.y * b + b); //标准设备坐标转屏幕坐标
    const p = new Vector2(vx, vy)

    v2.copy(p)
    return p
}

// 屏幕坐标转经纬度
export const coordsToLngLats = (point: Vector2, AMap: any, map: any, targetV2?: Vector2) => {
    const { x, y } = point
    var pixel = new AMap.Pixel(x, y);

    var lnglat = map.containerToLngLat(pixel);
    const v2 = new Vector2()
    v2.set(lnglat.lng, lnglat.lat)
    if (targetV2?.isVector2) {
        targetV2.copy(v2)
    }
    return v2

}
